package com.lemon.smartsafetynew.ui.adapter.notice;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.notice.NoticeItemVo;

import java.util.List;

import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Andrew on 2017/11/3.
 */

public class NoticeListNewAdapter extends LemonRcvAdapter<NoticeItemVo> {

    private RecyclerTClickListener<NoticeItemVo> mListener;

    public NoticeListNewAdapter(List<NoticeItemVo> data, RecyclerTClickListener<NoticeItemVo> listener) {
        super(data);
        this.mListener = listener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new NoticeItem();
    }

    public class NoticeItem implements AdapterItem<NoticeItemVo> {

        LinearLayout ripple;
        TextView tvItemTitle;
        TextView tvStatus;
        TextView tvUnit;
        TextView tvDate;
        ImageView ivFujian;
        TextView tvFujian;
        private NoticeItemVo mData;
        private int mPosition;


        @Override
        public int getLayoutResId() {
            return R.layout.item_notice_list;
        }

        @Override
        public void bindViews(View root) {
            ivFujian = (ImageView) root.findViewById(R.id.imageFujian);
            tvFujian = (TextView) root.findViewById(R.id.tvFujian);
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            tvItemTitle = (TextView) root.findViewById(R.id.tv_item_title);
            tvStatus = (TextView) root.findViewById(R.id.tv_status);
            tvUnit = (TextView) root.findViewById(R.id.tv_unit);
            tvDate = (TextView) root.findViewById(R.id.tv_date);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });
        }

        @Override
        public void handleData(NoticeItemVo data, int position) {
            mData = data;
            mPosition = position;
            String title;
            if(StringUtils.getBoolean(data.getType())){
                title = "[最新动态]";
            }else{
                title = "[最新文件]";
            }
            tvItemTitle.setText(title+StringUtils.nullValue(data.getTitle()));
            if (StringUtils.getBoolean(data.getIs_read())) {
                tvStatus.setText(StringUtils.getStatusString("已读", StringUtils.ColorStatus.STATUS_GREEN));
            } else {
                tvStatus.setText(StringUtils.getStatusString("未读", StringUtils.ColorStatus.STATUS_RED));
            }
            tvUnit.setText(StringUtils.nullValue(data.getRecorder_name()));
            tvDate.setText(StringUtils.friendly_time(com.lemon.smartsafetynew.util.common.CommonUtil.timeToStr(data.getRecord_date())));
            if(!TextUtils.isEmpty(data.getFiles())){
                String[] files = data.getFiles().split(",");
                tvFujian.setVisibility(View.VISIBLE);
                ivFujian.setVisibility(View.VISIBLE);
                tvFujian.setText(""+files.length);
            }else{
                tvFujian.setVisibility(View.GONE);
                ivFujian.setVisibility(View.GONE);
            }
        }
    }
}
